package com.xiaouyudeguang.common.typeHandler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes(JdbcType.DECIMAL)
public class BigDecimalTypeHandler extends org.apache.ibatis.type.BigDecimalTypeHandler {

    @Override
    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return super.getNullableResult(rs, columnName) == null ? BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP) : super.getNullableResult(rs, columnName).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return super.getNullableResult(rs, columnIndex) == null ? BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP) : super.getNullableResult(rs, columnIndex).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return super.getNullableResult(cs, columnIndex) == null ? BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP) : super.getNullableResult(cs, columnIndex).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
